package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.参数检测
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);//如果参数为空，返回无效参数501
        }
        dto.checkParam();//检查当前页和页数
        //2.安装名称模糊分页查询
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();//用LambdaQueryWrapper可以解决硬编码问题
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        IPage result = page(page, lambdaQueryWrapper);
        ResponseResult<Object> responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)result.getTotal());
        responseResult.setData(result.getRecords());
        //3.结果封装
        return responseResult;
    }

    @Override//新增频道
    public ResponseResult insert(AdChannel adChannel) {
        //1.检查参数
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);//如果参数为空，返回无效参数501
        }
        adChannel.setCreatedTime(new Date());//新增时要注意时间要添加
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Override//修改频道
    public ResponseResult update(AdChannel adChannel) {
        //1.检查参数
        if(adChannel==null || adChannel.getId()==null){//修改时一般按照id来修改，所以不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);//如果参数为空，返回无效参数501
        }
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Override//删除频道
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);//如果参数为空，返回无效参数501
        }
        AdChannel adChannel = getById(id);
        if(adChannel==null){//判断频道是否存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(adChannel.getStatus()){//频道如果有效不能修改
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }
//        int i=10/0;
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
